<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <template>
        <el-table :data="artCateList" style="width: 100%" border>
          <el-table-column type="index" label="序号" width="80px">
          </el-table-column>
          <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
          <el-table-column prop="cate_alias" label="分类别名">
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template #default="{ row, $index }">
              <el-button
                size="mini"
                type="primary"
                @click="getArtCateDetail(row.id)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="delArtCateList(row.id,$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <!-- 添加文章分类的对话框 -->
    <el-dialog
      @close="$refs.addRef.resetFields()"
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addRef"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input
            v-model="addForm.cate_name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="addForm.cate_alias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArtCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加文章分类的对话框 -->

    <!-- 修改文章分类的对话框 -->
    <el-dialog title="修改文章分类" :visible.sync="editVisible" width="35%">
      <el-form
        :model="artCateDetail"
        :rules="addRules"
        ref="editRef"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input
            v-model="artCateDetail.cate_name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input
            v-model="artCateDetail.cate_alias"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  created() {
    this.getArtCate();
  },
  data() {
    return {
      artCateDetail: { cate_name: "", cate_alias: "" },
      // 控制编辑文章对话框的显示和隐藏
      editVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: "",
        cate_alias: "",
      },
      //   控制对话框的显示和隐藏
      dialogVisible: false,
      //文章分类的数据
      artCateList: [],
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur",
          },
        ],
        cate_alias: [
          { required: true, message: "请输入分类别名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: "分类别名必须是1-15位的字母数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 删除文章列表数据
    delArtCateList(id,index) {
      console.log(index,'index');
      if (index === 0 || index === 1)
        return this.$message.error("管理员不允许删除这个分类！");
      this.$confirm("此操作将永久删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
          const { data: res } = await this.$http.delete(
            "/my/cate/del?id=" + id
          );
          console.log(res, "delArtCateList");
          if (res.code !== 0) return this.$message.error(res.message);
          this.$message.success(res.message);
          this.getArtCate();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 修改文章列表数据
    editCate() {
      this.$refs.editRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "/my/cate/info",
          this.artCateDetail
        );
        console.log(res, "editCate");
        if (res.code !== 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        // 关闭对话框
        this.editVisible = false;
        // 重新渲染页面
        this.getArtCate();
      });
    },
    // 根据id获取文章分类的数据
    async getArtCateDetail(id) {
      const { data: res } = await this.$http.get("/my/cate/info", {
        params: { id },
      });
      console.log(res.data, "getArtCateDetail");
      this.artCateDetail = res.data;
      this.editVisible = true;
    },
    // 添加文章列表数据
    addArtCate() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/my/cate/add",
          this.addForm
        );
        console.log(res, "addArtCate");
        if (res.code !== 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        // 关闭对话框
        this.dialogVisible = false;
        // 重新渲染页面
        this.getArtCate();
      });
    },
    // 发送请求获取文章列表数据
    async getArtCate() {
      const { data: res } = await this.$http.get("/my/cate/list");
      console.log(res.data, "getArtCate");
      this.artCateList = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>